#pragma once

#ifndef LHJ_CORE_SERVICES_SERVICE_H__
#define LHJ_CORE_SERVICES_SERVICE_H__

#include <string>
#include <map>
#include <memory>
#include <cstdint>
#include <vector>

namespace cores
{
    namespace services
    {
        using namespace std;
        class BaseService
        {
        public:
            BaseService() = delete;
            /**
             * @brief 构造函数
             */
            BaseService(const string &serviceName);

            /**
             * @brief 析构函数
             */
            virtual ~BaseService();

            /**
             * @brief 初始化
             */
            virtual void init();

            /**
             * @brief 启动服务
             */
            virtual void start();

            /**
             * @brief 停止服务
             */
            virtual void stop();

            /**
             * @brief 判断服务状态
             */
            bool isRunning() const;

            /**
             * @brief 获取服务名称
             */
            string name() const;

        protected:
            /**
             * @brief 服务运行状态
             */
            volatile bool _running{false};

        private:
            string _serviceName;
        };

        class ServiceManager
        {
        public:
            ServiceManager();
            virtual ~ServiceManager();

            static BaseService *Instance(const string &serviceName);
            BaseService *getBaseServiceInstance(const string &serviceName);

            static void AddService(BaseService *servicePtr);
            void addService(BaseService *servicePtr);
            
            static void RemoveService(BaseService *servicePtr);
            void removeService(BaseService *servicePtr);

            static void InitServices();
            void initServices();

            static void StartAllServices();
            void startAllServices();

            static void StartService(const string& serviceName);
            void startService(const string& serviceName);

            static bool ServiceIsRunning(const string& serviceName);
            bool serviceIsRunning(const string& serviceName);

            static void StopAllServices();
            void stopAllServices();

            static void StopService(const string& serviceName);
            void stopService(const string& serviceName);

            static vector<string> GetServieNames();
            vector<string> getServiceNames() const; 

        protected:
            map<string, BaseService *> _services;

        private:
            static ServiceManager *globalInstance;
        };
    }
}

#endif